#include <mfapi.h>
#include <mfidl.h>
#include <mfobjects.h>
#include <mftransform.h>
#include <mfreadwrite.h>
#include <shlwapi.h>
#include <wrl/client.h>
#include <iostream>

#pragma comment(lib, "mfplat.lib")
#pragma comment(lib, "mf.lib")
#pragma comment(lib, "mfuuid.lib")
#pragma comment(lib, "shlwapi.lib")

using namespace Microsoft::WRL;

int main() {
    HRESULT hr = MFStartup(MF_VERSION);
    if (FAILED(hr)) {
        std::cout << "MFStartup failed\n";
        return -1;
    }

    IMFActivate** ppActivate = nullptr;
    UINT32 count = 0;

    // 枚举所有视频编码器
    hr = MFTEnumEx(
        MFT_CATEGORY_VIDEO_ENCODER,
        MFT_ENUM_FLAG_ALL, // 可包含硬件、软件、可加载的MFT
        nullptr,           // 输入类型限制（可选）
        nullptr,           // 输出类型限制（可选）
        &ppActivate,
        &count
    );

    if (SUCCEEDED(hr)) {
        std::cout << "Found " << count << " video encoder(s):\n";
        for (UINT32 i = 0; i < count; ++i) {
            WCHAR* friendlyName = nullptr;
            UINT32 cchName = 0;

            IMFAttributes* pAttributes = nullptr;
            hr = ppActivate[i]->GetAllocatedString(MFT_FRIENDLY_NAME_Attribute, &friendlyName, &cchName);
            if (SUCCEEDED(hr)) {
                wprintf(L"[%u] %s\n", i, friendlyName);
                CoTaskMemFree(friendlyName);
            }

            // 释放激活对象
            ppActivate[i]->Release();
        }
        CoTaskMemFree(ppActivate);
    }
    else {
        std::cout << "MFTEnumEx failed\n";
    }

    MFShutdown();
    return 0;
}
